package com.mazaiting;

import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtLocalVars;
import com.mazaiting.rtda.MtOperandStack;
import lombok.extern.slf4j.Slf4j;

/**
 * 应用启动类
 * 测试参数:
 * -Xjre "/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre" java.lang.String
 *
 * @author mazaiting
 * @since 2021/8/10
 */
@Slf4j
public class SJava {
    public static void main(String[] args) {

        Cmd cmd = Cmd.parseCmd(args);
        if (cmd.isVersionFlag()) {
            System.out.println("version 0.0.1");
        } else if (cmd.isHelpFlag()) {
            log.info("Usage: {} [-options] class [args...]", SJava.class.getSimpleName());
        } else {
            startJVM(cmd);
        }
    }

    /**
     * 启动 JVM
     *
     * @param cmd 命令行参数
     */
    private static void startJVM(Cmd cmd) {
        MtFrame frame = new MtFrame(100, 100);
        testLocalVars(frame.getLocalVars());
        testOperandStack(frame.getOperandStack());
    }

    /**
     * 测试局部变量表
     * @param vars 局部变量表
     */
    private static void testLocalVars(MtLocalVars vars) {
        log.debug("===========测试本地变量表===========");
        vars.setInt(0, 100);
        vars.setInt(1, -100);
        vars.setLong(2, 2997924580L);
        vars.setLong(4, -2997924580L);
        vars.setFloat(6, 3.1415926F);
        vars.setDouble(7, 2.71828182845D);
        vars.setRef(9, null);
        log.debug(String.valueOf(vars.getInt(0)));
        log.debug(String.valueOf(vars.getInt(1)));
        log.debug(String.valueOf(vars.getLong(2)));
        log.debug(String.valueOf(vars.getLong(4)));
        log.debug(String.valueOf(vars.getFloat(6)));
        log.debug(String.valueOf(vars.getDouble(7)));
        log.debug(String.valueOf(vars.getRef(9)));
    }

    /**
     * 测试操作数栈
     * @param stack 操作舒展
     */
    private static void testOperandStack(MtOperandStack stack) {
        log.debug("===========测试操作数栈===========");
        stack.pushInt(100);
        stack.pushInt(-100);
        stack.pushLong(2997924580L);
        stack.pushLong(-2997924580L);
        stack.pushFloat(3.1415926F);
        stack.pushDouble(2.71828182845D);
        stack.pushRef(null);

        log.debug(String.valueOf(stack.popRef()));
        log.debug(String.valueOf(stack.popDouble()));
        log.debug(String.valueOf(stack.popFloat()));
        log.debug(String.valueOf(stack.popLong()));
        log.debug(String.valueOf(stack.popLong()));
        log.debug(String.valueOf(stack.popInt()));
        log.debug(String.valueOf(stack.popInt()));
    }
}


















